import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue')
  },
  {
    path: '/',
    name: 'admin',
    component: () => import('../views/Management.vue'),
    children: [
      {
        path: '/articlemanage',
        name: 'articlemanage',
        component: () => import('../components/ArticleManage.vue'),
      },
      {
        path: '/videomanage',
        name: 'videomanage',
        component: () => import('../components/VideoManage.vue'),
      },
      {
        path: '/discussmanage',
        name: 'discussmanage',
        component: () => import('../components/DiscussManage.vue'),
      },
      {
        path: '/usermanage',
        name: 'usermanage',
        component: () => import('../components/UserManage.vue'),
      },
    ],
  },
  {
    path: '/:pathMatch(.*)',
    component: () => import('../views/error/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 配置一个全局的前置路由守卫
router.beforeEach(function (to, from) {
  if (to.path == '/login') {
    return;
  } else {
    //从会话存储空间获取
    if (!sessionStorage.getItem("token")) { //没有
      //跳转到登录
      return { name: 'login' }
    }
  }
});

export default router
